import { reqGoodsInfo , reqAddOrUpdateshopcart} from "@/api";
//封装游客身份模块uuid--->生成一个随机字符串(一台电脑只能生成一个)
import {getUUID} from '@/utils/uuid_token.js'

const state = {
  goodInfo: {},
  //游客临时身份
  uuid_token:getUUID()
};
const mutations = {
  GETGOODINFO(state,goodInfo){
    state.goodInfo = goodInfo;
  }
};
const actions = {
  //获取产品信息的active 
  async getGoodInfo ({ commit }, skuid) {
    let result = await reqGoodsInfo(skuid);
    if(result.code == 200){
      commit("GETGOODINFO",result.data);
    }
  },
  //将产品添加到购物车中
  async addOrUpdateShopcart ({commit},{skuid,skuNum}){
    //加入购物车返回的解构
    let result = await reqAddOrUpdateshopcart(skuid,skuNum);
    //代表服务器加入购物车成功
    if(result.code == 200){
      return "ok";
    }
    else{
      //代表加入购物车失败
      return Promise.reject(new Error('faile'));
    }
  }
};
//简化数据而生
const getters = {
  //路径导航简化数据
  categoryView(state){
    //比如:state.goodInfo初始状态空对象,空对象的categoryView属性值undefined
    //当前计算出的categoryView属性值至少是一个空对象,假的报错不会有了。
    return state.goodInfo.categoryView || {};
  },
  //简化商品信息的数据
  skuInfo(state){
    return state.goodInfo.skuInfo || {};
  },
  //产品售卖属性的简化
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList ||[];
  }
};

export default {
  state,
  mutations,
  actions,
  getters
}